<?php 

class Utils {

	/**
	 * Czy to helper czy co? może metoda modelu albo validacji???
	 */
	
	static public function matches($r,$FLDS,$emptyToNull = null,$columns = null,$blockColumns = null){
		if(!is_array($r))$r = $r->getParams();
		
		foreach ($r as $k => $v){
			$match = false;
			foreach($FLDS as $fld){
				if($fld == $k && ($columns ? (array_key_exists($k,$columns) && $columns[$k]): true)){
					
					if(is_array($emptyToNull) && count($emptyToNull)>0 && Utils::isIn($k,$emptyToNull) && $v==''){
						$r[$k]=null;
						// był  unset wszystkich pustych - teraz wstawiamy null by usunąc poprzednią wartość
					} //else
					
					if(!(is_array($blockColumns) && count($blockColumns)>0 && Utils::isIn($k,$blockColumns))){
						$match = true;
					}
				}
			}
			
			if(!$match) unset($r[$k]);
			
		} return $r;
	}
	
	static public function rq($param = null,$value = '<magic_null>'){
		if(is_object($param)){// use less invention - z obiektu jak z hasza
			return self::rq(self::o2a($param));
		} elseif(self::isHash($param)){// setowanie wielu z hasha
			foreach($param as $k => $v){
				self::rq($k,$v);	
			}
		} elseif(is_array($param)){// getowanie wielu jako hash
			$ret = array();
			foreach($param as $k){
				$ret[$k] = self::rq($k);	
			}
			return $ret;
		} else {// własciwa operacja
			$rq = Zend_Controller_Front::getInstance()->getRequest();
			return $param == null ? $rq : ($value == '<magic_null>' ? 
				$rq->getParam($param) : $rq->setParam($param,$value));
		}
	}
	
	/**
	 * Mail
	 */
	static public function sendMail($to,$subject,$body){
		return Util_Mail::send($to,$subject,$body);
	}
	
	/**
	 * File
	 */
	static public function handleUpload($fieldName){
		return Util_File::handleUpload($fieldName);
	}
	
	/**
	 * CSV
	 */
	static public function csvHeaders($fileName){
		!$fileName && ($fileName = 'Eksport');
		
		header('Content-type: application/octet-stream');
		header("Content-Transfer-Encoding: binary"); 
		header('Expires: '.gmdate('D, d M Y H:i:s').' GMT'); 
		header('Pragma: no-cache'); 
    	header("Content-Disposition: attachment; filename='$fileName.csv'");
	}
	
	static public function csvServe($CSV){
		$owner_name =  strtoupper(Utils::config('instance'));
		$CSV = "Instancja: \t $owner_name \t \r\n" . $CSV;
		echo chr(255).chr(254).iconv("UTF-8", "UTF-16LE//IGNORE", $CSV); 
	}
	
	static public function csvPrepare($q,$flds){
		
		$CSV = implode("\t",array_keys($flds)) . "\t\r\n";
		while($n = $q->fetchObject()){
			$row = array();
			foreach($flds as $k => $v){
				array_push($row, $n->$v);
			}
			$CSV .= implode("\t",$row) . "\t\r\n";
		}
		return $CSV;
	}
	
	static public function csvExport($q,$flds,$fileName = null){
		self::csvHeaders($fileName);
		self::csvServe(self::csvPrepare($q, $flds));
	}
	
	/**
	 * Setup
	 */
	static public function getSetup($param){
		return Util_Setup::get($param);
	}
	
	static public function setSetup($param,$value){
		return Util_Setup::set($param,$value);
	}
	
	static public function addSetup($data){
		return Util_Setup::add($data);
	}
	
	static public function reg($var,$val = '<magic_null>'){
		return Util_Setup::registry($var,$val);
	}
	
	static public function config($cfgs = null){
		return Util_Setup::config($cfgs);
	}
	
	/**
	 * Log
	 */
	static public function logVeryHard($str='',$hash=array()){
		Util_Log::veryHard($str,$hash);		
	}
	
	static public function logHard($hash){
		Util_Log::hard($hash);
	}
	
	static public function log($msg,$a1='',$a2=''){
		Util_Log::fire($msg,$a1,$a2);
	}
	
	static public function fireSql($adapter){
		return Util_Log::fireSql($adapter);
	}
	
	static public function dump($var){
		return Util_Log::dump($var);
	}
	
	static public function handleError($errno,$errstr,$errfile,$errline,$errcontext){
		return Util_Log::handleError($errno,$errstr,$errfile,$errline,$errcontext);
	}
	
	/**
	 * USER
	 */	
	static public function whoAmI(){
		return Util_User::whoAmI();
	}	
	
	static public function myLogin(){
		return Util_User::myLogin();
	}
	
	static public function myId(){
		return Util_User::myId();
	}
		
	/**
	 * TODO 
	 * decyzja jaki alias funkcji zostawić
	 */
	static public function isMeAdmin(){
		return Utils::isIn(Utils::whoAmI(),array(Enum_Roles::Administrator,Enum_Roles::Developer));
	}
	
	static public function adminOrDie(){
		return self::isMeAdmin()? true : Validation::react('Operacja administracyjna');
	}
	
	/**
	 * Logic
	 */
	static public function isIn($needle,$array){
		return Util_Logic::isIn($needle,$array);
	}
	
	static public function allGood($array){
		return Util_Logic::allGood($array);
	}
	
	static public function allAre($val, $array){
		return Util_Logic::allAre($val,$array);
	}
	
	static public function allNumeric($array){
		return Util_Logic::allNumeric($array);
	}
	
	static public function compareHashVals($home_array,$away_array,$attr_array,$compareType = 'all_equal'){
		return Util_Logic::compareHashVals($home_array,$away_array,$attr_array,$compareType);
	}
	
	static public function isInstance($obj,array $classes){
		return Util_Logic::isInstance($obj,$classes);
	}
	
	static public function isAjax(){
		return Util_Logic::isAjax();
	}
	
	static public function isProduction(){
		return !Utils::isIn($_SERVER['HTTP_HOST'],array(
			'localhost'
		));
	}
	
	static public function rqTest($param){
		return Util_Logic::rqTest($param);
	}
	
	/**
	 * Array
	 */
	static public function isHash($arr) {
		return is_array($arr) && (array_keys($arr) !== range(0, count($arr) - 1));
	}
	
	static public function removeElement($array,$value){
		return Util_Array::removeElement($array,$value);
	}
	
	static public function arrayFetch($array){
		return Util_Array::fetch($array);
	}
	
	static public function a2o($a, $o = NULL) {
		return Util_Array::a2o($a,$o);
	}
	
	static public function h2s($glue,$separator,$array){// separator między name value pairy
		return Util_Array::h2s($glue,$separator,$array);
	}

	/**
	 * Where
	 */
	static public function createWhereStatement($where = null,$adapter = null){
		return Util_Where::createWhereStatement($where,$adapter);
	}
	
	static public function getValueFromWhere($param,$where=null){
		return Util_Where::getValueFromWhere($param,$where);
	}
	
	/**
	 * DB
	 */
	static public function translate($class,$id,$nameField){
		return Util_DB::translate($class,$id,$nameField);
	}
	
	static public function dbCase($col,$val = null,$then= null,$else = null){
		if($val===null)$val = 'NULL'; 
		if($then===null)$then = 'NULL';
		if($else===null)$else = 'NULL';
		return " (CASE $col WHEN $val THEN $then ELSE $else END) ";
	}

	function dbDateAdd($date,$amount,$unit = null){
		if(!$unit)$unit = 'WEEK';
		return " DATE_ADD($date,INTERVAL $amount $unit) ";
	}
	
	static public function quoteInto($template,$params){
		return Util_DB::quoteInto($template,$params);
	}
		
	static public function quote($sql,$params){
		return Util_DB::quote($sql,$params);
	} 
	
	static public function insert($table,$data){
		return Util_DB::insert($table,$data);
	}
	
	static public function table($class){
		return Util_DB::table($class);
	}
		
	/**
	 * Helper
	 */
	static public function getLastDateOfMonth($year,$month){
		return Util_Helper::getLastDateOfMonth($year,$month);
	}
	
	static public function getFirstDateOfMonth($y=false,$m=false){
		return Util_Helper::getFirstDateOfMonth($y,$m);
	}
	
	static public function monthWordByCount($c){
		return Util_Helper::monthWordByCount($c);
	}

	static public function rateWordByCount($c){
		return Util_Helper::rateWordByCount($c);
	}
	
	/**
	 * Devel (większość pominięta - bo nieuzywana w kodzie)
	 */
	static public function soapClient(){
		return Util_Devel::soapClient();
	}
	
}

?>